home *** CD-ROM | disk | FTP | other *** search
- Path: cymbal.aix.calpoly.edu!not-for-mail
- From: dstubbs@cymbal.aix.calpoly.edu (Dan Stubbs)
- Newsgroups: comp.lang.c
- Subject: Re: quick decision: is n a power of 2?
- Date: 27 Jan 1996 12:48:27 -0800
- Organization: California Polytechnic State University, San Luis Obispo
- Message-ID: <4ee32r$18e1@cymbal.aix.calpoly.edu>
- References: <Pine.OSF.3.91.960119114608.18779E-100000@io.UWinnipeg.ca> <4e1aeb$1gl8@cymbal.aix.calpoly.edu> <822516891snz@genesis.demon.co.uk> <4e8asi$ehg@ns.RezoNet.NET>
- NNTP-Posting-User: dstubbs@cymbal.aix.calpoly.edu
-
- In article <4e8asi$ehg@ns.RezoNet.NET>, Ray Dunn <ray@ultimate-tech.com> wrote:
- >In referenced article, Lawrence Kirby says...
-
- >>>int is_power_of_two(int k) { /* 1.0 1.0 1.0 1.0 */
- >>> if (k <= 0) return 0;
- >>> return (!(k & (k-1)));
- >>>}
- >>>int is_power_of_two(int k) { /* 1.1 1.0 1.1 1.0 */
- >>> if (k <= 0) return 0;
- >>> return ((k & (k-1)) == 0);
- >>>}
- >>I wouldn't be too impressed these days with a compiler that generates
- >>less efficient code for ((k & (k-1) == 0) than (!(k & (k-1))
- >
- >Yes, that was my reaction too, but looking at it, the differences are
- >in the noise level for this sort of timing which usually varies quite a
- >bit - especially when using a test that only runs for 1 second.
-
- When that data was posted it was pointed out that it was normalzed. For
- example, the times needed by four techniques (two of them shown above) to
- check the first 5,000,000 integers to see which of them is a power of 2
- was 185 1,333 167 341. The difference between 167 and 185 comes from
- the difference in how the compiler (set to its highest optimization level)
- treated the two expressions noted above. Unfortunately, I think there is
- a real difference.
-
-
-
-
-
-